﻿using Device.API.Applcation.Command.CarriersCommand;
using Device.Infrstructre.Interface;
using MediatR;

namespace Device.API.Applcation.CommandHandlers.CarriersCommandHandlers
{
    /// <summary>
    /// 删除承运商中介者
    /// </summary>
    public class DelCarriersModelCommandHandler : IRequestHandler<DelCarriersModelCommand, int>
    {
        private readonly ICarrierRepository _carrierRepository;
        //构造函数
        public DelCarriersModelCommandHandler(ICarrierRepository carrierRepository)
        {
            _carrierRepository = carrierRepository;
        }

        public async Task<int> Handle(DelCarriersModelCommand request, CancellationToken cancellationToken)
        {
            var code = 0;
            foreach (var item in request.id)
            {

                var res = (await _carrierRepository.GetAllAsync()).FirstOrDefault(x => x.CarriersId == item);
                if (res != null)
                {
                    res.IsDel = true;//假删
                    res.DelName = request.DelName;//删除人姓名
                    res.DelTime = request.DelTime;//删除人时间
                    await _carrierRepository.UpdateAsync(res);//修改
                    code = 1;
                }
                else
                {
                    code = -1;
                }
            }
            return code;

        }
    }
}
